package com.ft.adbix.core.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ft.adbix.core.R;
import com.ft.adbix.core.interactive.ShakeDetector;
import com.ft.adbix.core.listener.SplashListener;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.core.widget.InteractionLayout;
import com.ft.adbix.core.widget.ShakeInteractiveView;
import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.AdOpener;
import com.ft.adbix.utils.AdTrackerReporter;
import com.ft.adbix.utils.UIUtils;
import com.ft.adbix.utils.universalimageloader.core.ImageLoader;

public class SplashAdView extends FrameLayout {
    private Ad adData;
    private FrameLayout layoutContainer;
    private ImageView ivPic;
    private TextView tvCountDown;
    private InteractionLayout layoutInteraction;
    private SplashListener listener;
    private AdOpener adOpener;
    private AdTrackerReporter reporter;
    private int countDownTime = 5;
    private final String countdownFormat;
    private final Runnable countDown = new Runnable() {
        @Override
        public void run() {
            if (countDownTime > 0) {
                countDownTime--;
                tvCountDown.setText(String.format(countdownFormat, countDownTime));
                postDelayed(this, 1000);
            } else {
                if (listener != null) {
                    listener.onAdClose();
                }
            }
        }
    };

    public SplashAdView(@NonNull Context context) {
        this(context, null);
    }

    public SplashAdView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SplashAdView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.adbix_splash_view, this);
        ivPic = findViewById(R.id.adbix_splash_iv_image);
        layoutContainer = findViewById(R.id.adbix_fl_container);
        layoutInteraction = findViewById(R.id.adbix_fl_interaction);
        tvCountDown = findViewById(R.id.adbix_lib_tv_count_down);
        countdownFormat = getResources().getString(R.string.adbix_splash_skip_count_down);

    }

    public void renderView(Ad adData, SplashListener listener) {
        this.adData = adData;
        this.listener = listener;
        reporter = new AdTrackerReporter(adData);
        adOpener = new AdOpener(getContext(), adData, reporter);
        layoutInteraction.setReporter(reporter);

        tvCountDown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAdSkip();
                }
            }
        });
        layoutInteraction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                AdLogUtils.d("click = " + reporter.getMarkInfo());
                if (listener != null) {
                    listener.onAdClick();
                }
                reporter.reportClick();
                openAd();
            }
        });

//        if (adData.getVideo() == null) {
//            renderPic();
//        } else {
//            renderVideo();
//        }
        renderInteractive();
        renderPic();
    }

    private void renderInteractive() {
        //摇一摇
        ShakeInteractiveView shakeInteractiveView = new ShakeInteractiveView(getContext());
        shakeInteractiveView.setShakeListener(new ShakeDetector.OnShakeListener() {
            @Override
            public void onShake() {
                openAd();
            }
        });
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM;
        params.bottomMargin = UIUtils.dp2px(getContext(), 30);
        layoutInteraction.addView(shakeInteractiveView, params);
    }

    private void openAd(){
//        if (countDown != null) {
//            removeCallbacks(countDown);
//        }
        adOpener.openAd();
    }

    private void renderVideo() {

    }

    private void renderPic() {
//            ImageLoader.getInstance().displayImage(adData.getImageUrl(), ivPic);
        ImageLoader.getInstance().displayImage(adData.getImageUrl(), ivPic);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        reporter.reportDisplay();
        post(countDown);
        if (listener != null) {
            listener.onAdShow();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (countDown != null) {
            removeCallbacks(countDown);
        }
        if (listener != null) {
            listener.onAdClose();
        }
    }
}
